package com.shenyue.nbtownlet.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.trycatch.mysnackbar.Prompt;
import com.trycatch.mysnackbar.TSnackbar;

public abstract class BaseFragment extends Fragment {

    public String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String name = getArguments().getString("name");
        this.name = name;
    }

    public abstract void refresh(JSONObject data);

    public void setLoading(boolean loading) {
    }

    public abstract void willBeDisplayed();

    public void toActivity(final Class<?> cls) {
        toActivity(cls, false);
    }

    public void toActivity(final Class<?> cls, final boolean closeFlag) {
        try {
            final Context mContext = getActivity();
            Intent intent = new Intent();
            intent.setClass(mContext, cls);
            getActivity().startActivity(intent);
            if (closeFlag)
                getActivity().finish();
        } catch (Exception e) {

        }
    }

    public void toActivityForResult(Class<?> cls, Bundle bundle,
                                    int requestCode) {
        try {
            Intent intent = new Intent();
            intent.setClass(getActivity(), cls);
            if (bundle != null)
                intent.putExtras(bundle);
            getActivity().startActivityForResult(intent, requestCode);
        } catch (Exception e) {

        }
    }

    public void toActivity(final Class<?> cls, final Bundle bundle,
                           final boolean closeFlag) {
        try {
            final Context mContext = getActivity();
            Intent intent = new Intent();
            intent.setClass(mContext, cls);
            if (bundle != null)
                intent.putExtras(bundle);
            getActivity().startActivity(intent);
            if (closeFlag)
                getActivity().finish();
        } catch (Exception e) {

        }
    }

    public void toActivity(final Class<?> cls, final Bundle bundle) {
        toActivity(cls, bundle, false);
    }

    public void showMsg(String msg) {
        View view = getActivity().findViewById(R.id.container);
        TSnackbar.make(view, msg, TSnackbar.LENGTH_SHORT, TSnackbar.APPEAR_FROM_TOP_TO_DOWN).setPromptThemBackground(Prompt.WARNING).show();
    }

    public void showMsgS(String msg) {
        View view = getActivity().findViewById(R.id.container);
        TSnackbar.make(view, msg, TSnackbar.LENGTH_SHORT, TSnackbar.APPEAR_FROM_TOP_TO_DOWN).setPromptThemBackground(Prompt.SUCCESS).show();
    }

    public TSnackbar showTSnackbar(String msg) {
        View view = getActivity().findViewById(R.id.container);
        final TSnackbar snackBar = TSnackbar.make(view, msg, TSnackbar.LENGTH_INDEFINITE, TSnackbar.APPEAR_FROM_TOP_TO_DOWN);
        snackBar.setPromptThemBackground(Prompt.SUCCESS);
        snackBar.addIconProgressLoading(0, true, false);
        snackBar.show();
        return snackBar;
    }

    public void hideTSnackbar(TSnackbar snackBar, String msg, Prompt prompt) {
        if (snackBar == null)
            return;
        snackBar.setDuration(TSnackbar.LENGTH_SHORT);
        snackBar.setText(msg);
        snackBar.setPromptThemBackground(prompt);
        snackBar.show();
    }

    public void hideTSnackbar(TSnackbar snackBar, String msg) {
        hideTSnackbar(snackBar, msg, Prompt.SUCCESS);
    }
}
